#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LIST 116

#if (LIST == 15)

#include <stdio.h>
int main(void)
{
    int array[] = {1,2,3,4,5,6};

    signed int i = -1;
    if (i <= (int)sizeof(array)) // 强制类型转换，防止比较类型不一致，而int类型转换为unsigned int类型
    {
        printf(" i <= sizeof(array)\n");
    }
    else
    {
        printf(" i > sizeof(array)\n");
    }
    return 0;
}

#elif (LIST == 19)

/* 代码清单1-9 无符号整数运算示例 */
int main(void)
{
    unsigned int a = 4294967295;
    unsigned int b = 2;
    unsigned int c = 4;
    if(__UINT32_MAX__ - a < b)
    {
        /* 错误处理 */
    }
    else
        printf("%u\n",a + b);

    if(b < c)
    {
        /* 错误处理 */
    }
    else
        printf("%u\n",b - c);
    return 0;
}

#elif(LIST == 111) 

int main(int argc,char *argv[])
{
    unsigned short s;
    int i;
    char buf[100];

    if(argc < 3)
    {
        return -1;
    }

    i = atoi(argv[1]);
    s = i;
    if(s >= 100)
    {
        printf("拷贝字节数太大,请退出!\n");
        return -1;
    }
    printf("s = %d\n",s);
    memcpy(buf,argv[2],i);
    buf[i] = '\0';
    printf("成功拷贝%d个字节\n", i);
    printf("buf=%s\n", buf);
    return 0;
}

#elif(LIST == 112) 
// 1-12
int* copyarray(int *arr, int len);

int main(int argc, char *argv[])
{
    int arr[] = {1,2,3,4,5};
    copyarray(arr,atoi(argv[1]));
    return 0;
}

int* copyarray(int *arr, int len)
{
    int i = 0;
    if(len < 0x100000000 / 4))
    int *newarray = (int *)malloc(len*sizeof(int));
    if(newarray == NULL)
    {
        /*处理newarray == NULL的情况*/
    }

    printf("Successfully allocated %d bytes of memory for newarray\n", len*sizeof(int));
    printf("Number of loop runs: %d(0x%x)\n",len,len);
    for(i = 0; i < len; i++)
    {
        newarray[i] = arr[i];
    }
    return newarray;
}

#elif(LIST == 113) 
// 1-13
#include <stdio.h>
int main(void)
{
    int s1 = 2147483647;
    int s2 = 1073741824;
    int s3 = -1879048193;
    int s4 = 1;
    int s5 = 4;
    printf("%d(0x%x)+%d(0x%x)=%d(0x%x)\n", s1, s1, s4, s4, s1+s4, s1+s4);
    printf("%d(0x%x)-%d(0x%x)=%d(0x%x)\n", s2, s2, s3, s3, s2-s3, s2-s3);
    printf("%d(0x%x)*%d(0x%x)=%d(0x%x)\n", s2, s2, s5, s5, s2*s5, s2*s5);
    return 0;
}

#elif(LIST == 114) 
// 1-14
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int s1 = 2147483647;
    int s2 = 1073741824;
    int s3 = -1879048193;
    int s4 = 1;
    if(((s1^s4) | (((s1^(~(s1^s4)&(1 << (sizeof(int)*CHAR_BIT - 1)))) + s4)^s4)) >= 0)
    {
        /*处理溢出条件*/
    }
    else
    {
        printf("%d(0x%x)+%d(0x%x)=%d(0x%x)\n", s1, s1,s4,s4, s1+s4, s1+s4);
    }
    if(((s2^s3) & (((s2^((s2^s3)&(1 << (sizeof(int)*CHAR_BIT-1)))) - s3)^s3)) < 0)
    {
        /*处理溢出条件*/
    }
    else
    {
        printf("%d(0x%x)-%d(0x%x)=%d(0x%x)\n", s2, s2, s3, s3, s2-s3, s2-s3);
    }
    return 0;
}

#elif(LIST == 115) 
// 1-15
#include <stdio.h>
int main(void)
{
    int si1 = 1073741824;
    int si2 = 0;
    int si3 = -1073741824;
    int si4 = 4;
    int si5 = -1;
    printf("si1 = %d (0x%x)\n", si1, si1);
    si2 = si1 + si3;
    printf("si1 + %d(0x%x) = %d (0x%x)\n",si3,si3, si2, si2);
    si2 = si1 * si4;
    printf("si1 *  %d(0x%x) = %d (0x%x)\n",si4,si4, si2, si2);
    si2 = si1 - si5;
    printf("si1 - %d(0x%x) = %d (0x%x)\n",si5,si5, si2, si2);
    return 0;
}

#elif(LIST == 116) 
// 1-16
int copychar(char *c1,int len1, char *c2, int len2);

int main(int argc, char *argv[])
{
    copychar(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]));
    return 0;
}

int copychar(char *c1, int len1, char *c2, int len2)
{
    char buf[100];
    if((len1 + len2) > 100)
    {
            printf("超出buf容纳范围(100)!\n");
            return -1;
    }
    memcpy(buf, c1, len1);
    memcpy(buf+len1, c2, len2);
    printf("复制%d+%d=%d个字节到buf!\n",len1,len2,len1+len2);
    printf("buf=%s\n", buf);
    return 0;
}

#endif

